package com.clkj.tramcarlibrary.fragment.toNanmen;

import android.support.annotation.NonNull;
import android.util.Log;

import com.clkj.tramcarlibrary.entity.JinzhanDetail;
import com.clkj.tramcarlibrary.entity.ToNmShoumoDetail;
import com.clkj.tramcarlibrary.http.HttpService;
import com.clkj.tramcarlibrary.schedulers.BaseSchedulerProvider;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/2/14.
 */

public class ToNmPresenter implements ToNmContract.Presenter {

    @NonNull
    ToNmContract.View mToNmView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeSubscription mSubscriptions;

    public ToNmPresenter(@NonNull ToNmContract.View mToNmView, @NonNull BaseSchedulerProvider mSchedulerProvider, @NonNull HttpService mHttpService) {
        this.mToNmView = mToNmView;
        this.mSchedulerProvider = mSchedulerProvider;
        this.mHttpService = mHttpService;
        this.mSubscriptions = new CompositeSubscription();

        this.mToNmView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
        mSubscriptions = null;
    }

    //获取进站信息
    @Override
    public void getToNmJinzhan() {
        mToNmView.showLoading();
        Subscription subscription = mHttpService.getToNmJinzhanDetail()
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mToNmView.dismissLoading();
                            mToNmView.showToNmJinzhanError("服务器获取进站信息失败,请重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("失败", "失败");
                        mToNmView.dismissLoading();
                        mToNmView.showError("服务器获取数据异常");
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        Log.e("去南门进站", jsonObject.toString());
                        mToNmView.dismissLoading();
                        JsonArray jsonArray = jsonObject.get("data").getAsJsonArray();
                        List<JinzhanDetail> jinzhanDetails = new Gson().fromJson(jsonArray, new TypeToken<List<JinzhanDetail>>() {
                        }.getType());

                        mToNmView.showToNmJinzhan(jinzhanDetails);
                    }
                });
        mSubscriptions.add(subscription);
    }

    //获取首末班信息
    @Override
    public void getToNmShoumo() {
        mToNmView.showLoading();
        Subscription subscription = mHttpService.getToNmShoumoDetail()
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {

                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mToNmView.dismissLoading();
                            mToNmView.showToNmJinzhanError("服务器获取首末班信息失败,请重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("失败", "失败");
                        mToNmView.dismissLoading();
                        mToNmView.showError("服务器获取数据异常");
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        Log.e("去南门首末班", jsonObject.toString());
                        mToNmView.dismissLoading();
                        JsonArray jsonArray = jsonObject.get("data").getAsJsonArray();

                        List<ToNmShoumoDetail> shoumoDetails = new Gson().fromJson(jsonArray, new TypeToken<List<ToNmShoumoDetail>>() {
                        }.getType());

                        mToNmView.showToNmShoumo(shoumoDetails);

                    }
                });
        mSubscriptions.add(subscription);
    }
}
